package web.controller.address_telefon;

import java.util.ArrayList;
import java.util.List;

import hibernate.pojo.User;
import hibernate.pojo_address_telefon.Address;
import hibernate.pojo_address_telefon.AddressBook;
import hibernate.pojo_address_telefon.AddressTypes;
import hibernate.pojo_address_telefon.TelephoneTypes;
import hibernate.util.HibernateUtil;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

public class CreateNewAddressRecordController {

	private static final Log logger = LogFactory.getLog(CreateNewTelephoneBookController.class);
	
	private String postalCode;
	private String streetNumber;
	private String name;
	private AddressBook addressBook;
	private AddressTypes addressTypes;
	private Long listelemeSelectedAddressTypeId = new Long(0);
	private List<SelectItem> addressTypesItemsList;
	private List<AddressTypes> addressTypesList;
	
	public CreateNewAddressRecordController() {
		// TODO Auto-generated constructor stub
	}
	
	public String insertNewAddressRecord(){
		
		String pageToRedirect = "";
		int userID;
		int addressBookID = 0;
		
		if(!validateStreetNumber()){
			logger.info("Adres gecerli degil!");
			pageToRedirect = ""; //show warning message
			return pageToRedirect;
		}
		
		try {
			
			FacesContext context = FacesContext.getCurrentInstance();
			userID = ((User)context.getExternalContext().getSessionMap().get("user")).getId();
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(AddressBook.class);
			criteria.add(Restrictions.eq("userID",userID));

			List<AddressBook> addressBookFromDB = criteria.list();
			if(addressBookFromDB == null || addressBookFromDB.size() == 0){
				logger.info("Kullanicinin kayitli adres defteri bulunmamaktadir!");
				pageToRedirect = "";  //show warning message
				return pageToRedirect;
			}
			if(addressBookFromDB != null || addressBookFromDB.size() == 1){ // kullanicinin kayitli telefon defteri varsa, telefon ekle
				
				addressBook = (AddressBook)criteria.list().get(0);
				addressBookID = addressBook.getId();
				
				Address address = new Address();
				address.setPostalCode(postalCode);
				address.setStreetNumber(streetNumber);
				address.setName(name);
				address.setAddressBookID(addressBookID);
				address.setAddressBook(addressBook);
				if(getListelemeSelectedAddressTypeId() != null){
					int selectedAddressTypeIdAtDb = (int) (getListelemeSelectedAddressTypeId()-1);
					address.setAddressTypeID((selectedAddressTypeIdAtDb+1));
					address.setAddressType(addressTypesList.get(selectedAddressTypeIdAtDb));
				
				}
				
				Transaction transaction = session.beginTransaction();
				session.save(address);
				transaction.commit();
				clearFields();
				pageToRedirect = "createdNewAddressEntrySuccess";
				
			}
			
			
		} catch (Exception exception) {
			logger.error("Yeni adres kaydi yaparken hata olustu!", exception);
		}
		
		
		return pageToRedirect;
	}
	
	public boolean validateStreetNumber(){
		
		
		return true;
	}
	
	public void clearFields(){
		this.postalCode = "";
		this.streetNumber = "";
		this.name = "";
	}
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public String getStreetNumber() {
		return streetNumber;
	}

	public void setStreetNumber(String streetNumber) {
		this.streetNumber = streetNumber;
	}

	public AddressBook getAddressBook() {
		return addressBook;
	}

	public void setAddressBook(AddressBook addressBook) {
		this.addressBook = addressBook;
	}

	public AddressTypes getAddressTypes() {
		return addressTypes;
	}

	public void setAddressTypes(AddressTypes addressTypes) {
		this.addressTypes = addressTypes;
	}

	public Long getListelemeSelectedAddressTypeId() {				
		return listelemeSelectedAddressTypeId;
	}

	public void setListelemeSelectedAddressTypeId(
			Long listelemeSelectedAddressTypeId) {
		this.listelemeSelectedAddressTypeId = listelemeSelectedAddressTypeId;
	}

	public void setAddressTypesItemsList(List<SelectItem> addressTypesItemsList) {
		this.addressTypesItemsList = addressTypesItemsList;
	}

	public List<AddressTypes> getAddressTypesList() {
		return addressTypesList;
	}

	public void setAddressTypesList(List<AddressTypes> addressTypesList) {
		this.addressTypesList = addressTypesList;
	}
	
	public List<SelectItem> getAddressTypesItemsList() {
		
		if (addressTypesItemsList == null) {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(AddressTypes.class);
			addressTypesList = criteria.list();
			addressTypesItemsList = addressTypesListToSelectItemList(addressTypesList);
		}
		return addressTypesItemsList;
	}
	
	public static List<SelectItem> addressTypesListToSelectItemList(List<AddressTypes> addressTypesList) {

		List<SelectItem> returnAddressTypesSelectItemList = new ArrayList<SelectItem>();
		returnAddressTypesSelectItemList.add(new SelectItem(new Long(0), "Seçiniz"));
		if (addressTypesList != null && addressTypesList.size() > 0) {
			for (AddressTypes addressTypes : addressTypesList) {
				SelectItem selectItem = new SelectItem(addressTypes.getId(), addressTypes.getDescription());
				returnAddressTypesSelectItemList.add(selectItem);
			}
		}
		return returnAddressTypesSelectItemList;
	}

}
